package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiT_shipper_account;

import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiT_shipper_accountService;
import cn.com.api.service.StartOnLoadService;
import cn.com.api.entity.*;

import cn.com.util.Tools;
import cn.com.util.*;

public class T_memberBaseController {

	private static Logger logger = Logger
			.getLogger(T_memberBaseController.class);

	@Autowired
	private ApiT_memberService apiT_memberService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_member-list")
	public ModelAndView t_member_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_member_list.jsp");

		List<ApiT_member> t_members = apiT_memberService.findT_members();
		mv.addObject("t_members", t_members);

		return mv;
	}


	
	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_member-add-pre")
	public ModelAndView t_member_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_member_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_member t_member = apiT_memberService.findT_memberById(Integer
					.valueOf(id));
			mv.addObject("t_member", t_member);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_member-add")
	public void t_member_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String c_id = request.getParameter("c_id");
		String c_userid = request.getParameter("c_userid");
		String c_pass = request.getParameter("c_pass");
		String c_wt = request.getParameter("c_wt");
		String c_da = request.getParameter("c_da");
		String c_nick = request.getParameter("c_nick");
		String c_tname = request.getParameter("c_tname");
		String c_city = request.getParameter("c_city");
		String c_area = request.getParameter("c_area");
		String c_quan = request.getParameter("c_quan");
		String c_mob = request.getParameter("c_mob");
		String c_mail = request.getParameter("c_mail");
		String c_qq = request.getParameter("c_qq");
		String c_sex = request.getParameter("c_sex");
		String c_ip = request.getParameter("c_ip");
		String c_regdate = request.getParameter("c_regdate");
		String c_logdate = request.getParameter("c_logdate");
		String c_jf = request.getParameter("c_jf");
		String c_avatar = request.getParameter("c_avatar");
		String c_cname = request.getParameter("c_cname");
		String c_faren = request.getParameter("c_faren");
		String c_license = request.getParameter("c_license");
		String c_pname = request.getParameter("c_pname");
		String c_address = request.getParameter("c_address");
		String c_zip = request.getParameter("c_zip");
		String c_tel = request.getParameter("c_tel");
		String c_fax = request.getParameter("c_fax");
		String c_mtype = request.getParameter("c_mtype");
		String c_invite = request.getParameter("c_invite");
		String t_card = request.getParameter("t_card");
		String c_money = request.getParameter("c_money");
		String token = request.getParameter("token");
		String c_age = request.getParameter("c_age");
		String c_thirdtype = request.getParameter("c_thirdtype");
		String c_thirdtoken = request.getParameter("c_thirdtoken");
		String openid = request.getParameter("openid");
		String devicetype = request.getParameter("devicetype");
		String deviceid = request.getParameter("deviceid");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_member t_member = new ApiT_member();
			/*
			 * t_member.setC_id(c_id); t_member.setC_userid(c_userid);
			 * t_member.setC_pass(c_pass); t_member.setC_wt(c_wt);
			 * t_member.setC_da(c_da); t_member.setC_nick(c_nick);
			 * t_member.setC_tname(c_tname); t_member.setC_city(c_city);
			 * t_member.setC_area(c_area); t_member.setC_quan(c_quan);
			 * t_member.setC_mob(c_mob); t_member.setC_mail(c_mail);
			 * t_member.setC_qq(c_qq); t_member.setC_sex(c_sex);
			 * t_member.setC_ip(c_ip); t_member.setC_regdate(c_regdate);
			 * t_member.setC_logdate(c_logdate); t_member.setC_jf(c_jf);
			 * t_member.setC_avatar(c_avatar); t_member.setC_cname(c_cname);
			 * t_member.setC_faren(c_faren); t_member.setC_license(c_license);
			 * t_member.setC_pname(c_pname); t_member.setC_address(c_address);
			 * t_member.setC_zip(c_zip); t_member.setC_tel(c_tel);
			 * t_member.setC_fax(c_fax); t_member.setC_mtype(c_mtype);
			 * t_member.setC_invite(c_invite); t_member.setT_card(t_card);
			 * t_member.setC_money(c_money); t_member.setToken(token);
			 * t_member.setC_age(c_age); t_member.setC_thirdtype(c_thirdtype);
			 * t_member.setC_thirdtoken(c_thirdtoken);
			 * t_member.setOpenid(openid); t_member.setDevicetype(devicetype);
			 * t_member.setDeviceid(deviceid);
			 */
			apiT_memberService.insertT_member(t_member);
		}
		// edit
		else {
			ApiT_member t_member = apiT_memberService.findT_memberById(Integer
					.valueOf(id));
			/*
			 * t_member.setC_id(c_id); t_member.setC_userid(c_userid);
			 * t_member.setC_pass(c_pass); t_member.setC_wt(c_wt);
			 * t_member.setC_da(c_da); t_member.setC_nick(c_nick);
			 * t_member.setC_tname(c_tname); t_member.setC_city(c_city);
			 * t_member.setC_area(c_area); t_member.setC_quan(c_quan);
			 * t_member.setC_mob(c_mob); t_member.setC_mail(c_mail);
			 * t_member.setC_qq(c_qq); t_member.setC_sex(c_sex);
			 * t_member.setC_ip(c_ip); t_member.setC_regdate(c_regdate);
			 * t_member.setC_logdate(c_logdate); t_member.setC_jf(c_jf);
			 * t_member.setC_avatar(c_avatar); t_member.setC_cname(c_cname);
			 * t_member.setC_faren(c_faren); t_member.setC_license(c_license);
			 * t_member.setC_pname(c_pname); t_member.setC_address(c_address);
			 * t_member.setC_zip(c_zip); t_member.setC_tel(c_tel);
			 * t_member.setC_fax(c_fax); t_member.setC_mtype(c_mtype);
			 * t_member.setC_invite(c_invite); t_member.setT_card(t_card);
			 * t_member.setC_money(c_money); t_member.setToken(token);
			 * t_member.setC_age(c_age); t_member.setC_thirdtype(c_thirdtype);
			 * t_member.setC_thirdtoken(c_thirdtoken);
			 * t_member.setOpenid(openid); t_member.setDevicetype(devicetype);
			 * t_member.setDeviceid(deviceid);
			 */
			apiT_memberService.updateT_member(t_member);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_member-delete")
	public void t_member_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_memberService.deleteT_member(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
